_Pragma("once");

#include "MultiPage.h"
#include "EditablePage.h"
#include "StringListEditor.h"
#include "CurrentVoltageManager.h"
#include "common.h"

/**
 * 电流电压显示页面
 *
 * @author lizb
 */
class CurrentVoltagePage :public MultiPage<EditablePage> {
    using super =  MultiPage<EditablePage>;
//    static const std::string INVALID_VALUE = "000/000.0";
    static const int PORT_COUNT_PER_PAGE = 3;

public:
    CurrentVoltagePage(const int pageNum) : super(pageNum), mcurrentVoltageManager(CurrentVoltageManagerSingleton::instance()) {

    }


     void initialize() override{
    }

    void beforeDisplay() override {
        mportCount = mcurrentVoltageManager.updatePortCount();
        mpageSize = (mportCount + PORT_COUNT_PER_PAGE - 1)/ PORT_COUNT_PER_PAGE;

        super::beforeDisplay();

        initEditors();
    }


    void afterDisplay() override {
        editUnits.clear();

        super::afterDisplay();
    }


    void initEditors() override {
        int num = computePageItems(mportCount);

//		根据回路数量设置同等数量的可编辑单元
        StringListEditor* pEditor;
        for (int i = 0; i < num; ++i) {
            pEditor = new StringListEditor(i + 1, 4, 4, threePhaseVector);
            editUnits.push_back(BasicEditorSharedPtr(pEditor));
        }
    }


    void paint() override;

    void setValue() override {
        size_t portNo;
        size_t editorCount = editUnits.size();
        std::string phases;
        mcurrentVoltageManager.getPhase(phases);
        for (size_t i = 0; i < editorCount; ++i) {
            portNo = mmultiPageNo * PORT_COUNT_PER_PAGE + i;
            static_cast<StringListEditor*> (editUnits[i].get())->setValue(phases[portNo] - 'A');
        }
    }

    void saveValue() override {

        size_t portNo;
        size_t editorCount = editUnits.size();
        std::string phases;
        mcurrentVoltageManager.getPhase(phases);
        for (size_t i = 0; i < editorCount; ++i) {
            portNo = mmultiPageNo * PORT_COUNT_PER_PAGE + i;
            phases[portNo] = static_cast<StringListEditor*> (editUnits[i].get())->getText()[0];
        }
        mcurrentVoltageManager.setPhase(phases);
    }


private:

    /**
     * 通道数
     */
    int mportCount;
    CurrentVoltageManager& mcurrentVoltageManager;
};
